package com.ks.pc.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ks.code.ClassifyCode;
import com.ks.dto.ImgDto;
import com.ks.dto.OtherDto;
import com.ks.dto.ProductDto;
import com.ks.dto.ResultDto;
import com.ks.dto.UserInfoDto;
import com.ks.pc.service.SysBannerService;
import com.ks.pc.service.SysOtherService;
import com.ks.pc.service.SysProductService;
import com.ks.utils.paging.Page;

@Controller
@RequestMapping("/pc")
public class PcHomeController {

	@Autowired
	private SysProductService sysProductService;

	@Autowired
	private SysBannerService sysBannerService;

	@Autowired
	private SysOtherService sysOtherService;

	@RequestMapping(value = "/pcHome", method = RequestMethod.GET)
	public String pcHomeView(String brandId, String iemtSearchTxt, Model model) {
		model.addAttribute("brandId", brandId);	//左则导航菜单栏条件点入
		model.addAttribute("iemtSearchTxt", iemtSearchTxt);	//在item界面，头部点入
		return "biz/pcHome";
	}

	@RequestMapping(value = "/pcOnlineOpt", method = RequestMethod.GET)
	public String pcOnlineOptView(Model model) {
		return "biz/pcOnlineOpt";
	}

	@RequestMapping(value = "/pcContact", method = RequestMethod.GET)
	public String pcContactView(Model model) {
		OtherDto otherDto = sysOtherService.findOtherById(2);
		if (null != otherDto) {
			model.addAttribute(otherDto);
		}
		return "biz/pcContact";
	}

	@RequestMapping(value = "/pcAbout", method = RequestMethod.GET)
	public String pcAboutView(Model model) {
		OtherDto otherDto = sysOtherService.findOtherById(1);
		if (null != otherDto) {
			model.addAttribute(otherDto);
		}
		return "biz/pcAbout";
	}

	@RequestMapping(value = "/pcProductList", method = RequestMethod.GET)
	public String pcProductListView(Integer classifyId, Integer brandId, Model model) {
		if (null != classifyId && 9999 != classifyId) {
			model.addAttribute("classifyId", classifyId);
		}
		if (null != brandId && 9999 != brandId) {
			model.addAttribute("classifyId", classifyId);
			model.addAttribute("brandId", brandId);
		}
		return "biz/pcProductList";
	}

	@RequestMapping(value = "/pcIemt", method = RequestMethod.GET)
	public String pcIemtView(Integer productId, Model model) {
		model.addAttribute("productId", productId);
		return "biz/pcIemt";
	}

	@ResponseBody
	@RequestMapping(value = "/classifyBrandRel", method = RequestMethod.POST)
	public List<Map<String, Object>> classifyBrandRel() {
		return sysProductService.classifyBrandRel();
	}

	@ResponseBody
	@RequestMapping(value = "/findSlidePhotoByClassifyId", method = RequestMethod.POST)
	public List<ImgDto> findSlidePhotoByClassifyId(Integer classifyId) {
		List<ImgDto> list = sysBannerService.findSlidePhotoByClassifyId(classifyId);
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/findProductByClassifyId", method = RequestMethod.POST)
	public List<List<ProductDto>> findProductByClassifyId(String brandId, Model model) {
		Map<String, Object> param = new HashMap<String, Object>();
		// 菜单栏条件点入
		if (null != brandId) {
			param.put("brandId", brandId);
		}

		param.put("pageStart", 0);
		param.put("pageSize", 8);

		param.put("classifyId", ClassifyCode.PHONE);
		List<ProductDto> phoneDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.IPAD);
		List<ProductDto> padDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.CAMERA);
		List<ProductDto> cameraDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.PC);
		List<ProductDto> pcDtos = sysProductService.findProductByClassifyId(param);

		List<List<ProductDto>> products = new LinkedList<List<ProductDto>>();
		products.add(phoneDtos);
		products.add(padDtos);
		products.add(cameraDtos);
		products.add(pcDtos);

		List<List<ProductDto>> orderProducts = new LinkedList<List<ProductDto>>();
		for (int j = 1; j <= 4; j++) {

			for (int i = 0; i < products.size(); i++) {
				Integer showIndex = products.get(i).get(i).getShowIndexClassify();
				if (showIndex == j) {
					orderProducts.add(products.get(i));
					break;
				}
			}

		}
		return orderProducts;
		// Map<String, Object> rs = new HashMap<String, Object>();
		// rs.put("phoneDtos", phoneDtos);
		// rs.put("cameraDtos", cameraDtos);
		// rs.put("pcDtos", pcDtos);
		// rs.put("padDtos", padDtos);
		// return rs;
	}

	@ResponseBody
	@RequestMapping(value = "/findProductListByClassifyId", method = RequestMethod.POST)
	public Map<String, Object> findProductListByClassifyId(int pageNo, Integer classifyId, Integer brandId,
			String searchTxt, Model model) {

		Map<String, Object> param = new HashMap<String, Object>();
		if (null != classifyId) {
			param.put("classifyId", classifyId);
		}
		if (null != brandId) {
			param.put("brandId", brandId);
		}
		if (null != searchTxt && !("".equals(searchTxt))) {
			param.put("searchTxt", searchTxt);
		}

		int pageSize = 12;// 每页显示的行数
		int countItem = sysProductService.countProductByClassifyId(param);// 总数条数
		Page page = new Page(pageNo, pageSize, countItem);

		param.put("pageStart", page.getBegin());
		param.put("pageSize", pageSize);

		List<ProductDto> rs = sysProductService.findProductByClassifyId(param);

		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("productDtos", rs);
		rsMap.put("totalPage", page.getPageCount());
		rsMap.put("totalRecords", countItem);
		return rsMap;
	}

	@ResponseBody
	@RequestMapping(value = "/findProductBySearchTxt", method = RequestMethod.POST)
	public List<ProductDto> findProductBySearchTxt(String searchTxt, Model model) {
		if (null == searchTxt) {
			return null;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("searchTxt", searchTxt);
		List<ProductDto> pcDtos = sysProductService.findProductBySearchTxt(param);
		return pcDtos;
	}

	@ResponseBody
	@RequestMapping(value = "/findProductById", method = RequestMethod.POST)
	public ProductDto findProductById(Model model, Integer productId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("productId", productId);
		return sysProductService.findProductById(productId);
	}

	@ResponseBody
	@RequestMapping(value = "/insertOnlineOpt2", method = RequestMethod.POST)
	public ResultDto<String> insertOnlineOpt2(UserInfoDto userInfoDto) {
		ResultDto<String> result = new ResultDto<String>();
		result.setType(true);
		return result;
	}
	
//	@ResponseBody
//	@RequestMapping(value = "/insertOnlineOpt", method = RequestMethod.POST)
//	public ResultDto<String> insertOnlineOpt(String userName, String userPhoneNum, String userIDCard, String userStages,
//			String userNote) {
//		ResultDto<String> result = new ResultDto<String>();
//		UserInfoDto dto = new UserInfoDto();
//		dto.setUserName(userName);
//		dto.setUserPhoneNum(userPhoneNum);
//		dto.setUserIDCard(userIDCard);
//		dto.setUserStages(userStages);
//		dto.setUserNote(userNote);
//		result.setType(sysProductService.saveOnlineOpt(dto));
//		return result;
//	}
}
